/**
 * 
 */
package com.omega.controller;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.omega.common.utils.Utils;
import com.omega.constants.ApplicationConstants;
import com.omega.entity.Supplier;
import com.omega.service.SupplierService;
import com.omega.vo.SupplierList;

/**
 * @author vpan14
 * 
 */
@Controller
@RequestMapping(value = "/admin/supplier")
public class SupplierController {

	@Autowired
	SupplierService supplierService;

	/**
	 * 
	 */
	public SupplierController() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/addSupplier", method = RequestMethod.GET)
	public String showAddSupplierForm(Model model) {
		List<Supplier> supplierList = new CopyOnWriteArrayList<Supplier>();
		for (int i = 0; i < 5; i++) {
			supplierList.add(new Supplier());
		}
		SupplierList supplierListForm = new SupplierList();
		supplierListForm.setSupplierList(supplierList);
		model.addAttribute("yearMap", Utils.getYearsMap());
		model.addAttribute("monthMap", Utils.getMonthsMap());
		model.addAttribute("supplierListForm", supplierListForm);
		return ApplicationConstants.ADD_NEW_SUPPLIER_VIEW;
	}

	/**
	 * 
	 * @param form
	 * @param result
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addSupplier", method = RequestMethod.POST)
	public String submitAddSupplierForm(
			@ModelAttribute("supplierListForm") SupplierList supplierListForm,
			BindingResult result, Model model, HttpServletRequest request) {
		for (Supplier form : supplierListForm.getSupplierList()) {
			if (!(StringUtils.isEmpty(form.getTinNumber()))
					& !(StringUtils.isEmpty(form.getSupplierName()))) {
				supplierService.addSupplier(form);
			}
		}
		return ApplicationConstants.REDIRECT_SUPPLIERS_VIEW;
	}

	/**
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/viewSuppliers", method = RequestMethod.GET)
	public String viewAllSuppliers(Model model) {
		model.addAttribute("supplierList", supplierService.loadAllSuppliers());
		return ApplicationConstants.SUPPLIERS_VIEW;
	}

	/**
	 * 
	 * @param supplierId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/editSupplier/{supplierId}", method = RequestMethod.GET)
	public String showEditSupplierForm(@PathVariable String supplierId,
			Model model) {
		Supplier supplier = supplierService.loadById(Integer
				.parseInt(supplierId));
		model.addAttribute("yearMap", Utils.getYearsMap());
		model.addAttribute("monthMap", Utils.getMonthsMap());
		model.addAttribute("supplier", supplier);
		return ApplicationConstants.EDIT_SUPPLIER_VIEW;
	}

	/**
	 * 
	 * @param supplierId
	 * @param form
	 * @param result
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editSupplier/{supplierId}", method = RequestMethod.POST)
	public String submitEditSupplierForm(@PathVariable String supplierId,
			@ModelAttribute("supplier") Supplier form, BindingResult result,
			Model model, HttpServletRequest request) {
		supplierService.updateSupplier(form);
		return ApplicationConstants.REDIRECT_SUPPLIERS_VIEW;
	}

	/**
	 * 
	 * @param supplierId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/deactivateSupplier/{supplierId}", method = RequestMethod.GET)
	public String deactivateSupplier(@PathVariable String supplierId,
			Model model) {
		supplierService.deactivateSupplier(Long.parseLong(supplierId));
		return ApplicationConstants.REDIRECT_SUPPLIERS_VIEW;
	}

	/**
	 * 
	 * @param supplierId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/activateSupplier/{supplierId}", method = RequestMethod.GET)
	public String activateSupplier(@PathVariable String supplierId, Model model) {
		supplierService.activateSupplier(Long.parseLong(supplierId));
		return ApplicationConstants.REDIRECT_SUPPLIERS_VIEW;
	}

}
